<?php
namespace Home\Controller;
use Think\Controller;
class QuestionController extends CommonController {
	public function index()
	{	
		$id = I('get.id');
		$this->autoAdd('question','view_num',$id,1);
		
		$sql ="select t1.*,t2.nikename,t2.face from question t1 left join user t2 on t1.uid=t2.id where t1.id='{$id}'";

		$question = M()->query($sql);

        $sql = "select t1.*,t2.nikename,t2.face from answer t1 left join user t2 on t1.uid=t2.id where qid='{$id}'";

        $answer = M()->query($sql);

        $question = current($question);
        $this->assign('a',$answer);
		$this->assign('q',$question);
        $this->assign('title',$question['title']);
		$this->display();
	}
    public function add(){

    	$cateData = M('category')->select();
    	$cateData = $this->cateTree($cateData);
    	// var_dump($cateData);exit;
    	$question = D('vercode')->getOne();
    	$this->assign('question',$question);
    	$this->assign('cateData',$cateData);
        $this->display();
    }

    public function upload()
    {
    	$upload = new \Think\Upload();// 实例化上传类
	    $upload->maxSize   =     3145728 ;// 设置附件上传大小
	    $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
	    $upload->rootPath  =     './Uploads/pic/'; // 设置附件上传根目录
	    $upload->savePath  =     ''; // 设置附件上传（子）目录
	    // 上传文件 
	    $info   =   $upload->upload();

	    if(!$info) {// 上传错误提示错误信息
	    	$retrunData = array(
	    		'code'=>1,
	    		'msg'=>$upload->getError()
    		);
    		echo json_encode($retrunData);
	    }else{// 上传成功
	    	$info = current($info);
	        $retrunData = array(
	    		'code'=>0,
	    		'msg'=>'上传成功',
	    		'data'=>array(
	    			'src'=>__ROOT__."/Uploads/pic/".$info['savepath'].'/'.$info['savename'],
	    			'title'=>'图片'
	    			)
	    		);
	    	echo json_encode($retrunData);
	    }
    	
    }

    public function post()
    {
    	$this->checkVercode();
    	$data = I('post.');
    	$data['uid'] = $_SESSION['id'];
    	$data['create_time'] = time();

    	$res = M('question')->add($data);

    	if($res){
    		$url = U('home/question/index',array('id'=>$res));
    		$arr = array(
             'error'=>'0',
             'info'=>'发布成功',
             'url'=>$url,
             );
    		echo json_encode($arr);
    	}else{
    		$arr = array(
             'error'=>'1',
             'info'=>'发布失败，可能人品问题',
             );
    		echo json_encode($arr);
    	}
    }
}